---@class Battle.EntityRoadArrowSystem:Battle.EntityComponent
local EntityRoadArrowSystem = Battle.CreateClass("EntityRoadArrowSystem",Battle.EntityComponent)
Battle.EntityRoadArrowSystem = EntityRoadArrowSystem


function EntityRoadArrowSystem:OnCreate()
    self.loadRoadArrowId = -1
    ---@type RoadArrow
    self.arrow = nil
    self.pos = Vector3.New()
    self.resName = Battle.EFFECT_PATH.."eff_xunlu"
    self.isHand = false
    self:Register()
end
function EntityRoadArrowSystem:OnDestroy()
    self:UnRegister()
    self:ObjectToPool()
end
function EntityRoadArrowSystem:Register()
    local eventSystem = self.actor.eventSystem
    eventSystem:Register(Battle.Event.ActorHandMove,self.Event_HandMove,self)
    eventSystem:Register(Battle.Event.ActorHandMoveEnd,self.Event_HandMoveEnd,self)

end
function EntityRoadArrowSystem:UnRegister()
    local eventSystem = self.actor.eventSystem
    eventSystem:UnRegister(Battle.Event.ActorHandMove,self.Event_HandMove,self)
    eventSystem:UnRegister(Battle.Event.ActorHandMoveEnd,self.Event_HandMoveEnd,self)
end
---@param go GameObject
function EntityRoadArrowSystem:LoadCallback(go)
    self.arrow = go:GetComponent(typeof(RoadArrow))

end
function EntityRoadArrowSystem:UpdatePos()
    if self.arrow then
        local p = self.actor.pos
        self.arrow:SetStart(p.x,0.01,p.z)
        self.arrow:SetEnd(self.pos.x,0.02,self.pos.z)
        self.arrow:UpdateLine()
    end
end
function EntityRoadArrowSystem:ObjectToPool()
    Battle.PoolMgr.CancelLoad(self.loadRoadArrowId)
    if self.arrow then
        Battle.PoolMgr.ReturnPrefabToPool(self.resName,self.arrow.gameObject)
    end
    self.arrow = nil
    self.loadRoadArrowId = -1
end
---@param args BattleEvent
function EntityRoadArrowSystem:Event_HandMove(args)
    if self.loadRoadArrowId == -1 then
        self.loadRoadArrowId = Battle.PoolMgr.LoadPrefabFromPool(self.resName, self.LoadCallback, self)
    end
    self.pos:Set(args.x,args.y,args.z)
    self:UpdatePos()
    self:RegUpdate(true)
    self.isHand = true

end
function EntityRoadArrowSystem:Event_HandMoveEnd(args)
    self.isHand = false
    self.pos:Set(args.x,args.y,args.z)
    self:UpdatePos()
end
function EntityRoadArrowSystem:Update(dt)
    if self.isHand or self.actor.aiSystem:GetBlackboardValue("hand_move") then
        self:UpdatePos()
    else
        self:RegUpdate(false)
        self:ObjectToPool()
    end
end